///*****************************************
//  Copyright (C) 2009-2014
//  ITE Tech. Inc. All Rights Reserved
//  Proprietary and Confidential
///*****************************************
//   @file   <io.c>
//   @author Jau-Chih.Tseng@ite.com.tw
//   @date   2012/07/24
//   @fileversion: HDMIRX_SAMPLE_2.18
//******************************************/

//#include "Mcu.h"
#include <stdio.h>
#include "typedef.h"

#include "io.h"
#include "TimerProcess.h"
#include "hdmirx.h"
#include "linux_swap.h"

BYTE HDMIRX_ReadI2C_Byte(BYTE RegAddr)
{
    BYTE uc ;
    i2c_read_byte(HDMIRXADR, RegAddr, 1, &uc, RXDEV);
    return uc ;
}

SYS_STATUS HDMIRX_WriteI2C_Byte(BYTE RegAddr,BYTE val)
{
    if( i2c_write_byte(HDMIRXADR, RegAddr, 1, &val, RXDEV) == 0)
    {
        return ER_SUCCESS ;
    }
    return ER_FAIL ;

}

SYS_STATUS HDMIRX_ReadI2C_ByteN(BYTE RegAddr,BYTE *pData,int N)
{
    if( i2c_read_byte(HDMIRXADR, RegAddr,N, pData, RXDEV) )
    {
        return ER_SUCCESS ;
    }
    return ER_FAIL ;
}

SYS_STATUS HDMIRX_WriteI2C_ByteN(BYTE RegAddr,BYTE *pData,int N)
{
    if( i2c_write_byte(HDMIRXADR, RegAddr,N, pData, RXDEV) == 0)
    {
        return ER_SUCCESS ;
    }
    return ER_FAIL ;
}

#ifdef _EDID_Parsing_
BOOL EDID_READ_BYTE( BYTE address, BYTE offset, BYTE byteno, BYTE *p_data, BYTE device)
{
    return i2c_read_byte(address, offset, byteno, p_data, device);

}
BOOL EDID_WRITE_BYTE( BYTE address, BYTE offset, BYTE byteno, BYTE *p_data, BYTE device )
{
    return i2c_write_byte(address, offset, byteno, p_data, device);
}
#endif

//BOOL i2c_write_byte( BYTE address, BYTE offset, BYTE byteno, BYTE *p_data, BYTE device )
//{
//}
//
//
//BOOL i2c_read_byte( BYTE address, BYTE offset, BYTE byteno, BYTE *p_data, BYTE device )
//{
//}

///////////////////// Delay Utility ///////////////////////////
void delay1ms(USHORT ms)
{
	usleep(ms*1000);
}

BOOL RXHPD_ENABLE=TRUE ;
USHORT HPDOff_TickCount ;
#define HPDOFF_MIN_PERIOD 2000
void hdmirx_set_hpd()
{
    if( RXHPD_ENABLE == FALSE )
    {
        //while(CalTimer(HPDOff_TickCount)<HPDOFF_MIN_PERIOD)
        //{
        //    HDMIRX_DEBUG_PRINTF(("HPDOff_TickCount = %d, ucTickCount = %d\r",HPDOff_TickCount,ucTickCount)) ;
        //}
        HDMIRX_DEBUG_PRINTF(("\n")) ;

    	Turn_HDMIRX(ON) ;
	//FIXME:gpio pin read
        //RX_HPD = HIGH ;
        RXHPD_ENABLE = TRUE ;
    }
}

void hdmirx_clear_hpd()
{
    if( RXHPD_ENABLE == TRUE )
    {
        //HPDOff_TickCount = ucTickCount ;
    	Turn_HDMIRX(OFF) ;
	//FIXME:gpio pin read
        //RX_HPD = LOW ;
        RXHPD_ENABLE = FALSE ;
    }
}

BOOL DisableVGAChecking()
{
//    P1_6 = 1 ;
//    if( !P1_6 ) return TRUE ;
//    return FALSE ;
}
